import numpy
import sys,os,inspect
from PyQt4 import QtCore
from KTrade.KStockFeed import KStockFeed
from KModel.KSingleStock import KSingleStock
from KCharts.KGraphics.KBaseDisplayItem import KBaseDisplayItem,KBaseDisplayItemModel

class KBaseIndicator(KBaseDisplayItemModel):
	''' the base class of all indicators'''
	def __init__(self,parent=None):
		QtCore.QObject.__init__(self,parent)
		assert(isinstance(parent,KSingleStock)==True)
		self.m_stock=parent
		self.dataReady=False # denote data is ready or not
		self.PricePeriod=self.m_stock.GetCurPeriod() # remember period since once period change we need recalulate
		self.StockName=self.m_stock.GetSymbolName()
		
	## offline functions
	def calculate(self,**kargs):
		# use in offline mode, will be called when user add indicator into the chart
		pass
	
	## online functions
	def OnNewBars(self,curfeedindex):
		# used in online mode, will be called when a new bar is available
		pass
	def OnStart(self):
		# used in online mode, called before simulation started
		pass
	def OnFinish(self):
		# used in online mode, called after simulation exectued
		pass
	def SetupStockFeed(self,feed):
		self.stockfeed=feed

## general functions		
	def IsResultExpired(self):
		# check if current stock period is differnt with what we used for calculate
		if not self.m_stock.GetCurPeriod()==self.PricePeriod:
			return True
		if not self.StockName==self.m_stock.GetSymbolName():
			return True
		return False
	
	def IsReady(self):
		# tell if the data is ready
		return self.dataReady
			

	def update(self):
		# update data before calcuation
		
		self.dataReady=True
		self.PricePeriod=self.m_stock.GetCurPeriod() #remeber latest period used for compute
		self.StockName=self.m_stock.GetSymbolName()
	
class KBaseIndicatorDisplayItem(KBaseDisplayItem):
	'''base class of all indicator display items'''
	pass


	
		
